﻿//***********************************************************************
//* Module:  TbBstVersionDAL
// * 作者:  曹荣芝
//* Modified: 2013/5/26
// * Purpose: Definition of the Class TbBstVersionDAL
// ***********************************************************************/
using System;
using System.Data;
using XWG.Framework.AuthEntity;
using XWG.JWBST.JWBSTEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTSPConst;
using XWG;
using XWG.Framework.MSException;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate;
namespace XWG.JWBST.JWBSTDAL
{
    /// <summary>
    /// 
    /// </summary>
    public class TbBstVersionDAL
    {
        #region 删除操作
        /// <summary>
        /// 删除。
        /// </summary>
        /// <param name="versionid">要删除的的主键值。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>是否成功 1 表示成功 负值代表失败</returns>
        public static ResultModel Delete(object versionid, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();//定义错误信息类
            try
            {
                //载入实例
                JWBSTEntity.TbBstVersion u = JWBSTDAL.TbBstVersionDAL.Load(versionid) as JWBSTEntity.TbBstVersion;

                if (isLog)//如果写入日志
                {
                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "删除";
                    log.LogObject = TbBstVersionConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Db.TransDelete<TbBstVersion, AppLog>(u, log);
                }
                else//不写入日志
                {
                    Db.TransDelete(u);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(versionid);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9994;
                resultModel.FiledKey = Convert.ToString(versionid);
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的删除操作");
                return resultModel;

            }
        }
        #endregion

        #region 根据主键查询一条记录
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="versionid">主键</param>
        /// <returns>用户实体</returns>
        public static JWBSTEntity.TbBstVersion Load(object versionid)
        {
            return Db.GetModel<JWBSTEntity.TbBstVersion>(versionid);
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 查询
        /// </summary>
        public static IList<TbBstVersion> SelectList()
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstVersion));
                IList<TbBstVersion> list = crt.List<TbBstVersion>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {

            }
        }
        #endregion

        #region 插入操作
        /// <summary>
        /// 新增。
        /// </summary>
        /// <param name="info">待新增的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>新增是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Insert(JWBSTEntity.TbBstVersion info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            object filedkey;
            try
            {
                if (isLog)
                {

                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "新建";
                    log.LogObject = TbBstVersionConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    filedkey = Db.TransInsert<TbBstVersion, AppLog>(info, log);
                }
                else
                {
                    filedkey = Db.TransInsert<TbBstVersion>(info);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(filedkey);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9998;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的插入操作");
                return resultModel;
            }
        }
        #endregion

        #region 修改操作
        /// <summary>
        /// 修改。
        /// </summary>
        /// <param name="info">待修改的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>修改是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Update(JWBSTEntity.TbBstVersion info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                if (isLog)
                {


                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "修改";
                    log.LogObject = TbBstVersionConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Db.TransUpdate<TbBstVersion, AppLog>(info, log);
                }
                else
                {
                    Db.TransUpdate<TbBstVersion>(info);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {
                resultModel.ErrorCode = -9997;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的修改操作");
                return resultModel;
            }
        }
        #endregion

        /// <summary>
        /// 获取数据库最新版本的信息
        /// </summary>
        /// <returns></returns>
        public static XWG.JWBST.JWBSTEntity.TbBstVersion GetLastVno()
        {
            string sql = "select * from TB_BST_VERSION where VERSION_NUM = (select max(VERSION_NUM) from TB_BST_VERSION)";
            DataTable dt = OrclHelper.GetDT(sql);
            XWG.JWBST.JWBSTEntity.TbBstVersion verInfo = new TbBstVersion();
            if (dt.Rows.Count > 0 && dt != null)
            {
                verInfo.VersionId = dt.Rows[0]["VERSION_ID"].ToString();
                verInfo.VersionNum =Convert.ToInt32(dt.Rows[0]["VERSION_NUM"]);
                verInfo.VersionData = Convert.ToDateTime(dt.Rows[0]["VERSION_DATA"].ToString());
                verInfo.VersionUrl = dt.Rows[0]["VERSION_URL"].ToString();
                verInfo.Isenforce = dt.Rows[0]["ISENFORCE"].ToString();
                verInfo.Postedby = dt.Rows[0]["POSTEDBY"].ToString();
                verInfo.VersionRemark = dt.Rows[0]["VERSION_REMARK"].ToString();
                return verInfo;
            }
            else
            {
                return verInfo;
            }
        }
    }
}
